package com.system.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonString;
import com.system.pojo.Layui_tab_JSON;
import com.system.pojo.User;
import com.system.service.BookService.BookService;
import com.system.service.BookService.BookServiceImpl;
import com.system.service.UserService.UserService;
import com.system.service.UserService.UserServiceImpl;
import com.system.util.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

public class MainServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数 method
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String cmd = req.getParameter("cmd");
        System.out.println("获取到的cmd参数为： " + cmd);
        Object o = req.getSession().getAttribute(Constant.USER_SESSION);
        int userId = ((User)o).getUserId();
        System.out.println("session："+o.toString());
        System.out.println("userId :"+userId);

        if (cmd.equals("select")) {
            this.select(req, resp);
        }else if (cmd.equals("updatePsw")){
            this.updatePsw(req, resp,o);
        }else if(cmd.equals("type")){
            this.type(req, resp);
        }else if(cmd.equals("focus")){
            this.getUserPsw(req,resp,o);
        }

    }
    public void select(HttpServletRequest req,HttpServletResponse resp){
        System.out.println("我是：select");
        String interest = "";
        interest = req.getParameter("interest");
        String search = "";
        search = req.getParameter("search");
        String search1 = "";
        search1 = req.getParameter("search1");
        System.out.println("interest为："+interest);
        System.out.println("search为：："+search);
        System.out.println("search1为：："+search1);
        try {
            List<Object> list = null;
            BookService bookService = new BookServiceImpl();
            if(interest == null || search == null|| search1 == null){
                list = bookService.SelectBookList("", "", "");
            }else{
                list = bookService.SelectBookList(interest, search, search1);
            }
            System.out.println(list);
            //放入表格中
            Layui_tab_JSON layui = new Layui_tab_JSON();
            layui.setCode(0);
            layui.setCount(1000);
            layui.setMsg("");
            layui.setData(list);
            resp.getWriter().write(JSONObject.toJSONString(layui));
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }

    }
    public void updatePsw(HttpServletRequest request,HttpServletResponse response,Object user){
        try{
            String sourcePsw = request.getParameter("sourcePsw");
            String updatePsw = request.getParameter("updatePsw");
            System.out.println("sourcePsw为："+sourcePsw);
            System.out.println("updatePsw为："+updatePsw);
            UserService userService = new UserServiceImpl();
            int f = userService.UpdatePsw(updatePsw, ((User) user).getUserId());
            if(f != 0){
                request.getSession().removeAttribute(Constant.USER_SESSION);
                response.getWriter().write("ok");
            }else{
                response.getWriter().write("error");
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }
    public void type(HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("我是：type");
        BookService bookService = new BookServiceImpl();
        List<Object> type = null;
        try {
            type = bookService.SelectTypeList();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("servlet中的type"+type);
        response.getWriter().write(JSON.toJSONString(type));
    }
    public void getUserPsw(HttpServletRequest request,HttpServletResponse response, Object user) throws IOException {
        String sourcePsw = request.getParameter("sourcePsw");
        String userPsw = ((User)user).getUserPsw();
        System.out.println(userPsw);
        if(userPsw.equals(sourcePsw)){
            System.out.println("ok");
            response.getWriter().write("ok");
        }else{
            System.out.println("error");
            response.getWriter().write("error");
        }
    }
}
